import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from widgets import ParameterPanel, TotalCostDisplayWidget, CostCalculator, ConstructionGuide, PatternPreviewPage


class MainWindow(QMainWindow):
    """主窗口"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("智能铺装系统")
        self.setGeometry(100, 100, 1200, 800)
        self.init_ui()

    def init_ui(self):
        """初始化用户界面"""
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        # 主布局
        main_layout = QVBoxLayout(central_widget)

        # 创建参数输入面板
        self.parameter_panel = ParameterPanel(parent=self)
        main_layout.addWidget(self.parameter_panel)

        # 创建其他组件
        self.total_cost_display = TotalCostDisplayWidget(parent=self)
        self.cost_calculator = CostCalculator(parent=self)
        self.construction_guide = ConstructionGuide(parent=self)
        self.pattern_preview = PatternPreviewPage(parent=self)

        # 添加到布局
        main_layout.addWidget(self.total_cost_display)
        main_layout.addWidget(self.cost_calculator)
        main_layout.addWidget(self.construction_guide)
        main_layout.addWidget(self.pattern_preview)

        # 连接信号
        self.parameter_panel.pattern_confirmed.connect(self.on_pattern_confirmed)

    def on_pattern_confirmed(self, pattern_type, selected_modes):
        """处理图案确认信号"""
        # 更新其他组件
        self.pattern_preview.update_preview(pattern_type, selected_modes)
        self.construction_guide.update_guide(pattern_type, selected_modes)

        # 计算成本
        self.cost_calculator.calculate_cost(
            self.parameter_panel.site_length_input.value(),
            self.parameter_panel.site_width_input.value(),
            self.parameter_panel.brick_length_input.value(),
            self.parameter_panel.brick_width_input.value(),
            self.parameter_panel.brick_height_input.value(),
            self.parameter_panel.brick_price_input.value(),
            self.parameter_panel.sand_price_input.value(),
            self.parameter_panel.labor_price_input.value(),
            self.parameter_panel.stone_price_input.value(),
            self.parameter_panel.cement_price_input.value(),
            self.parameter_panel.mortar_price_input.value()
        )

        # 更新总成本显示
        self.total_cost_display.update_display(self.cost_calculator.total_cost)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    print("启动应用程序...")

    try:
        print("开始初始化主窗口...")
        window = MainWindow()
        print("设置窗口标题和尺寸...")
        print("创建参数输入区...")
        window.show()
        print("主窗口初始化成功")
        sys.exit(app.exec())
    except Exception as e:
        print(f"主窗口初始化失败: {str(e)}")
        sys.exit(1)